package com.zhika.zishi.activity;

import android.animation.ObjectAnimator;
import android.content.Intent;
import android.nfc.Tag;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.zhika.zishi.R;
import com.zhika.zishi.adapter.AnswerInfoAdapter;
import com.zhika.zishi.beans.AnswerInfoBean;
import com.zhika.zishi.customview.Bottom2View;
import com.zhika.zishi.customview.TitleView;
import com.zhika.zishi.interfaces.Bottom2Interface;
import com.zhika.zishi.interfaces.TitleInterface;
import com.zhika.zishi.util.LogUtil;

import java.util.ArrayList;

/**
 * Created by shi-02 on 2015/4/9.
 */
public class ProblemDetailsActivity extends BaseActivity implements View.OnClickListener,TitleInterface,Bottom2Interface{

    private static final String TAG = "ProblemDetailsActivity";

    private ImageView hideImageView;

    private ImageView showImageView;

    private ImageView headerImageView;

    private ListView listView;

    private AnswerInfoAdapter answerInfoAdapter;

    private LinearLayout problemInfoLayout;

    private int problemInfoLayoutHeight;

    private LinearLayout listViewLayout;

    private LinearLayout.LayoutParams listViewParams;

    private int listViewLayoutHeight;

    private Bottom2View bottomView;

    private TitleView titleView;

    private ArrayList<AnswerInfoBean> answerInfoList = new ArrayList<>();

    private TextView superaddText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.problem_details_layout);
        addTestData();
        initView();

        getWidgetSize();
    }

    /**
     * 初始化界面
     */
    private void initView(){

        superaddText = (TextView) findViewById(R.id.problem_details_superadd_text);
        Intent intent = getIntent();
        String superaddStr = "";
        if (intent.getStringExtra("superadd")!=null){
            superaddStr = intent.getStringExtra("superadd");
            superaddText.setVisibility(View.VISIBLE);
            superaddText.setText(superaddStr);
        }else {
            superaddText.setVisibility(View.GONE);
        }


        listView = (ListView) findViewById(R.id.problem_details_answer_list_view);
        answerInfoAdapter = new AnswerInfoAdapter(this,answerInfoList);
        listView.setAdapter(answerInfoAdapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(ProblemDetailsActivity.this,"回答内容被点击了",Toast.LENGTH_SHORT).show();
            }
        });

        hideImageView = (ImageView) findViewById(R.id.problem_details_hide_image_view);
        hideImageView.setOnClickListener(this);

        showImageView = (ImageView) findViewById(R.id.problem_details_show_image_view);
        showImageView.setOnClickListener(this);

        problemInfoLayout = (LinearLayout) findViewById(R.id.problem_details_problem_info_layout);

        listViewLayout = (LinearLayout) findViewById(R.id.problem_details_list_view_layout);
        listViewParams = (LinearLayout.LayoutParams) listViewLayout.getLayoutParams();

        bottomView = (Bottom2View) findViewById(R.id.problem_details_bottom_view);
        bottomView.setBottom2Interface(this);
        bottomView.hideMiddleRightLayout();
        bottomView.setLeftLayoutText("添加回答");
        bottomView.setRightLayoutText("邀请回答");

        titleView = (TitleView) findViewById(R.id.problem_details_title_view);
        titleView.setTitleViewInterface(this);
        titleView.setMiddleRowText("回答个数");
        titleView.setLeftButtonText("返回");
        titleView.setRightButtonText("更多");

        headerImageView = (ImageView) findViewById(R.id.problem_details_header_image_view);
        headerImageView.setOnClickListener(this);
    }

    /**
     * 得到控件的尺寸
     */
    private void getWidgetSize(){
        problemInfoLayout.post(new Runnable() {
            @Override
            public void run() {
                problemInfoLayoutHeight = problemInfoLayout.getHeight();
                LogUtil.e(TAG,"问题详情布局的高度："+problemInfoLayoutHeight);

                listViewLayoutHeight = listViewLayout.getHeight();
                LogUtil.e(TAG,"listVeiw布局的高度："+listViewLayoutHeight);
            }
        });
    }
    /**
     * 添加测试数据
     */
    private void addTestData(){
        for (int i = 0;i<10;i++){
            AnswerInfoBean bean = new AnswerInfoBean();
            bean.setAnswerContent("回答内容回答内容回答内容回答内容回答内容回答内容回答内容回答内容回答内容回答内容回答内容回答内容回答内容回答内容回答内容回答内容回答内容");
            bean.setHeaderUrl("JJ");
            bean.setLikeNumStr("99");
            bean.setNicknameStr("回答者昵称");
            bean.setPictureUrl("URL");

            answerInfoList.add(bean);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.problem_details_hide_image_view:
                hideImageViewListener();
                break;
            case R.id.problem_details_show_image_view:
                showImageViewListener();
                break;
            case R.id.problem_details_header_image_view:
                Intent intent = new Intent(ProblemDetailsActivity.this,TaInfoActivity.class);
                startActivity(intent);
                break;
            default:
                break;
        }
    }

    /**
     * 隐藏问题详情的事件
     */
    private void hideImageViewListener(){
        hideImageView.setVisibility(View.GONE);
        showImageView.setVisibility(View.VISIBLE);
        listViewParams.height = listViewLayoutHeight + problemInfoLayoutHeight;
        listViewLayout.setLayoutParams(listViewParams);
        ObjectAnimator.ofFloat(listViewLayout,"translationY",0f,-problemInfoLayoutHeight).setDuration(200).start();
//        problemInfoLayout.setVisibility(View.GONE);
    }

    /**
     * 显示问题详情的事件
     */
    private void showImageViewListener(){
        hideImageView.setVisibility(View.VISIBLE);
        showImageView.setVisibility(View.GONE);
        ObjectAnimator.ofFloat(listViewLayout,"translationY",-problemInfoLayoutHeight,0f).setDuration(200).start();

    }

    @Override
    public void leftTitleListener() {
        finish();
    }

    @Override
    public void middleTitleListener() {

    }

    @Override
    public void rightTitleListener() {

    }

    @Override
    public void leftBottom2Listener() {

    }

    @Override
    public void middleLeftBottom2Listener() {

    }

    @Override
    public void middleRightBottom2Listener() {
        Toast.makeText(ProblemDetailsActivity.this,"中右",Toast.LENGTH_SHORT).show();
    }

    @Override
    public void rightBottom2Listener() {

    }
}
